package com.pkk.spring.cloud.starter.rpc.converter;

import cn.hutool.core.date.DatePattern;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

/**
 * 消息转换处理器
 *
 * @author peikunkun
 * @version V1.0
 * @date 2021-01-06 17:36
 **/
public class MessageConverterConfig {


  /**
   * 使用jackson序列化消息转换
   *
   * @return org.springframework.boot.autoconfigure.http.HttpMessageConverters
   * @Param
   * @author peikunkun
   * @date 2021/1/6 0006 下午 6:09
   * @since
   */
  @Bean
  public MappingJackson2HttpMessageConverter fastJsonHttpMessageConverters() {
    MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
    messageConverter.setDefaultCharset(Charset.defaultCharset());

    //@formatter:off
    ObjectMapper objectMapper = new ObjectMapper();
    // 忽略json字符串中不识别的属性
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // 忽略无法转换的对象
    objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    // PrettyPrinter 格式化输出
    objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    // 指定时区
    objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    // 日期类型字符串处理
    objectMapper.setDateFormat(new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN));

    // java8日期日期处理
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
    javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
    javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
    javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
    javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
    javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
    objectMapper.registerModule(javaTimeModule);
    messageConverter.setObjectMapper(objectMapper);



    //支持的媒体类型
    List<MediaType> supportedMediaTypes = new LinkedList<>();
    supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    //页面直接请求的类型
    supportedMediaTypes.add(MediaType.TEXT_HTML);
    messageConverter.setSupportedMediaTypes(supportedMediaTypes);
    //@formatter:on
    return messageConverter;
  }

}
